// pages/quotation/info/info.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    info: {},
    height: "",
    swiperIndex: {},
    markers: [],
    latitude: '',
    longitude: '',
    moreHistory: false,
    modalName: '',
    checkbox: [],
    // checkbox: [{
    //     value: 0,
    //     name: '场地信息',
    //     checked: true,
    //   }, 
    //   {
    //     value: 1,
    //     name: '婚礼场地',
    //     checked: true,
    //   }, 
    //   // {
    //   //   value: 2,
    //   //   name: '套餐内容',
    //   //   checked: true,
    //   // }, 
    //   {
    //     value: 3,
    //     name: '婚宴臻选',
    //     checked: true,
    //   }, 
    //   // {
    //   //   value: 4,
    //   //   name: '必吃婚宴',
    //   //   checked: true,
    //   // }, 
    //   {
    //     value: 5,
    //     name: '婚礼入住',
    //     checked: true,
    //   },
    //   {
    //     value: 6,
    //     name: '额外惊喜',
    //     checked: true,
    //   }, {
    //     value: 7,
    //     name: '爆款婚礼',
    //     checked: true,
    //   }
    // ],
    query: '',
    auth: true,
    isShare: null,
    cardCur: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log('options', options)
    wx.hideShareMenu();
    this.query(options.id)
    this.setData({
      id: options.id,
      auth: options.time ? options.time > Number(new Date()) : true,
      query: options.query,
      isShare: options.isShare
    })
  },
  changeMoreHistory() {
    this.setData({
      moreHistory: !this.data.moreHistory
    })
  },
  query(id) {
    // 调用云函数
    wx.cloud.callFunction({
      name: 'getQuotation',
      data: {
        id
      },
      success: res => {
        console.log('[云函数] [getQuotation]: ', res.result.data);
        const {
          location,
          taocan_imgs,
          rqhlcdList,
          hlrzfxxzList,
          rmhyzxList,
          shareList,
          shareTaocan,
          shareRqhlcd,
          shareHlrzfxxz,
          shareRmhyzx,
          shareBchycdtj
        } = res.result.data
        let latitude = ''
        let longitude = ''
        if (location) {
          console.log(location)
          latitude = location.split(',')[1]
          longitude = location.split(',')[0]
        }

        let checkbox = [...shareList]
        if (shareTaocan && Array.isArray(shareTaocan)) {
          shareTaocan.forEach((item, index) => {
            checkbox.push({
              value: `taocan_${index}`,
              name: item,
              checked: false,
            })
          })
        }
        if (shareRqhlcd && Array.isArray(shareRqhlcd)) {
          shareRqhlcd.forEach((item, index) => {
            checkbox.push({
              value: `changdi_${index}`,
              name: item,
              checked: false,
            })
          })
        }
        if (shareHlrzfxxz && Array.isArray(shareHlrzfxxz)) {
          shareHlrzfxxz.forEach((item, index) => {
            checkbox.push({
              value: `ruzhu_${index}`,
              name: item,
              checked: false,
            })
          })
        }
        if (shareRmhyzx && Array.isArray(shareRmhyzx)) {
          shareRmhyzx.forEach((item, index) => {
            checkbox.push({
              value: `zhenxuan_${index}`,
              name: item,
              checked: false,
            })
          })
        }
        if (shareBchycdtj && Array.isArray(shareBchycdtj)) {
          shareBchycdtj.forEach((item, index) => {
            checkbox.push({
              value: `bchycdtj_${index}`,
              name: item,
              checked: false,
            })
          })
        }

        wx.setNavigationBarTitle({
          title: res.result.data.name
        })
        this.setData({
          info: res.result.data,
          latitude,
          longitude,
          markers: [{
            id: 1,
            latitude,
            longitude,
            // width: '24',
            // height: '24',
            // iconPath: '',
            callout: {
              content: res.result.data.address_name,
              padding: 5,
              // borderRadius: 2,
              display: 'ALWAYS',
              fontSize: 10,
              // color: '#a60046'
            },
          }],
          checkbox
        })
      },
      fail: err => {
        console.error('[云函数] [getWorks] 调用失败', err)
      }
    })
  },
  loadHeight: function (e) {
    console.log(e)
    var width = wx.getSystemInfoSync().windowWidth
    //获取可使用窗口宽度
    var imgheight = e.detail.height
    //获取图片实际高度
    var imgwidth = e.detail.width
    //获取图片实际宽度
    var height = width * imgheight / imgwidth + "px"
    //计算等比swiper高度
    this.setData({
      [e.currentTarget.dataset.key]: height
    })
  },
  goheight: function (e) {
    var width = wx.getSystemInfoSync().windowWidth
    //获取可使用窗口宽度
    var imgheight = e.detail.height
    //获取图片实际高度
    var imgwidth = e.detail.width
    //获取图片实际宽度
    var height = width * imgheight / imgwidth + "px"
    //计算等比swiper高度
    this.setData({
      height: height
    })
  },
  cardSwiper(e) {
    this.setData({
      swiperIndex: {
        ...this.data.swiperIndex,
        [e.currentTarget.dataset.key]: e.detail.current
      }
    })
  },
  // 打开地图
  onRunApi() {
    const version = wx.getSystemInfoSync().SDKVersion;
    console.log(version)
    const mapCtx = wx.createMapContext('myMap', this);
    const {
      latitude,
      longitude,
      info
    } = this.data
    mapCtx.openMapApp({
      latitude: Number(latitude),
      longitude: Number(longitude),
      destination: info.address_name,
      complete: res => {
        console.log(res);
      }
    });

  },
  showModal() {
    this.setData({
      modalName: 'ChooseModal'
    })
  },
  hideModal(e) {
    this.setData({
      modalName: null
    })
  },
  ChooseCheckbox(e) {
    let items = this.data.checkbox;
    let values = e.currentTarget.dataset.value;
    for (let i = 0, lenI = items.length; i < lenI; ++i) {
      if (items[i].value == values) {
        items[i].checked = !items[i].checked;
        break
      }
    }
    this.setData({
      checkbox: items
    })
  },
  cardSwiper1(e) {
    console.log(e)
    this.setData({
      cardCur:  e.detail.current
    })
  },
  goto(e) {
    console.log(e.currentTarget.dataset)
    const url = e.currentTarget.dataset.path
    console.log(url)
    wx.navigateTo({
      url
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (e) {
    const {
      shareName,
      shareUrl
    } = this.data.info
    const {
      id,
      checkbox
    } = this.data


    return {
      title: shareName,
      imageUrl: shareUrl, // 图片 URL
      path: `/pages/quotation/info/info?id=${id}&isShare=1&query=${checkbox.map(item => item.checked ? null: item.value)}&time=${new Date().getTime() + 1000 * 60 * 60 * 24 * 3}`
    }
  }
})